﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mani.Common.Security;
using RastinArgham.CRM.Business;

namespace RastinArgham.CRM.Web
{
    public partial class list_contacts : System.Web.UI.Page
    {
        public ISecurityManager securityManager { get; set; }
        public CRMRepository crmRepository { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (securityManager == null)
            {
                securityManager = new SecurityManager();
                securityManager.authProvider = new WebAuthorizationProvider();
            }

            if (crmRepository == null)
                crmRepository = new CRMRepository();


            if (!securityManager.IsActionPermitted("all_contacts") && !securityManager.IsActionPermitted("my_contacts"))
                Response.Redirect("~/access_denied.aspx");

            if (!Page.IsPostBack)
            {
                LoadCities();
                LoadContacts();
            }
        }

        private void LoadCities()
        {
            ddlCities.DataSource = crmRepository.FindCitiesByUser(securityManager.authProvider.GetCurrentUser().UserId);
            ddlCities.DataBind();
        }

        private void LoadContacts()
        {
            if (securityManager.IsActionPermitted("all_contacts"))
                LoadAllContacts();
            else
                LoadMyContacts();
        }

        private void LoadMyContacts()
        {
            List<Contact> contacts = new List<Contact>();
            Guid myId = securityManager.authProvider.GetCurrentUser().UserId;
            List<Guid> myAccountManagers = crmRepository.FindMyAccountManagers(myId);

            if (String.IsNullOrEmpty(txtSearch.Text))
                contacts = crmRepository.GetContacts(Convert.ToInt32(ddlCities.SelectedValue), myAccountManagers, myId);
            else
                contacts = crmRepository.SearchContacts(Convert.ToInt32(ddlCities.SelectedValue), myAccountManagers, myId, txtSearch.Text);

            lblCount.Text = contacts.Count.ToString();
            gvContacts.DataSource = contacts;
            gvContacts.DataBind();
        }

        private void LoadAllContacts()
        {
            List<Contact> contacts = new List<Contact>();

            if (String.IsNullOrEmpty(txtSearch.Text))
                contacts = crmRepository.GetContacts(Convert.ToInt32(ddlCities.SelectedValue));
            else
                contacts = crmRepository.SearchContacts(txtSearch.Text, Convert.ToInt32(ddlCities.SelectedValue));

            lblCount.Text = contacts.Count.ToString();
            gvContacts.DataSource = contacts;
            gvContacts.DataBind();
        }

        protected void gvContacts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvContacts.PageIndex = e.NewPageIndex;
            LoadContacts();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            gvContacts.PageIndex = 0;
            LoadContacts();
        }
    }
}